// pages/order/delivery/delivery.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchValue: "",
    list: [],
    active: 3,
    isLastPage: false,
    current: 1,
    size: 10,
		role: '',
  },
  //搜索
	onSearch(e) {
		this.setData({
			current: 1,
			list: [],
			isLastPage: false,
			searchValue: e.detail
		})

		this.getData();
	},
  getData() {
    let {
      current,
      size,
			role
    } = this.data;
    let parmas;
    //市场调用采购单分页
      parmas = {
        companyId: app.globalData.userInfo.companyData.companyId,
        current,
        size,
				goodsOwnerName:this.data.searchValue,
				// purchaseStatus:7 //发货完成
      }
    wx.showLoading({
      title: "正在请求数据中"
    })
		//marketerPage 备货单
		// if(role ==1){
		// 	parmas = {
    //     companyId: app.globalData.userInfo.companyData.companyId,
    //     current,
    //     size,
		// 		outStockStatus :4 
    //   }
		// }else{
		// 	parmas = {
		// 		outStockUserId : app.globalData.userInfo.userDetail.id ,
		// 		outStockStatus :4 ,
    //     companyId: app.globalData.userInfo.companyData.companyId,
    //     current,
    //     size,
    //   }
		// }
    wx.$apis.marketerPage(parmas).then(res => { //备货单分页 不用传递状态
      wx.stopPullDownRefresh();
      let old = this.data.list;
      let allList = res.data && res.data.records.map(item => {
				item.transactionPrice=wx.$getYuan(item.transactionPrice)
				item.createTime=item.createTime.replace('T',' ')
				item.goodsOwnerMobile=wx.$getMobile(item.goodsOwnerMobile)
        return item
      })
      if (allList && allList.length) {
        let newList = old.concat(allList)
        this.setData({
          list: newList
        })
        if (allList.length < size) {
          this.setData({
            isLastPage: true
          })
        }
      } else {
        this.setData({
          isLastPage: true
        })
      }
    }).catch(err => {
      wx.hideLoading()
    })
  },
		//跳转详情页面
		gotodetail(e) {
			console.log(e)
			let id = e.currentTarget.dataset.id
			let settleHas = e.currentTarget.dataset.settlehas
			// if(e.currentTarget.dataset.outstockstatus){
				wx.navigateTo({
					url: "/pages/dashboard/hzdetail/hzdetail?id=" + id+"&type=3&settleHas="+settleHas
				});
			// }else{
			// 	wx.showToast({
			// 		title: '该订单暂未发货',
			// 		icon: "none",
			// 		duration: 1000
			// 	})
			// }
	
			// if (this.data.type == 3) {
				//跳转货主详情页面
				// wx.navigateTo({
				// 	url: "/pages/dashboard/hzdetail/hzdetail?id=" + id+"&type=3"
				// });
			// } else {
			// 	//跳转农户详情页面
			// 	wx.navigateTo({
			// 		url: "/pages/dashboard/nhdetail/nhdetail"
			// 	});
			// }
		},
  //跳转货主页面
  gotohz(e) {
    console.log('name', e.currentTarget.dataset.name)
    const name = e.currentTarget.dataset.name;
    const purchaseOrderId = e.currentTarget.dataset.purchaseorderid;
    wx.navigateTo({
      url: "/pages/order/orderDetail/orderDetail?id=" + purchaseOrderId + "&name=" + name
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		this.setData({
			role: app.globalData.userInfo.role,
		})
    // this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
			this.onPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      current: 1,
      list: [],
      isLastPage: false
    })
    this.getData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {
      current,
      isLastPage
    } = this.data
    if (isLastPage) {
      return
    }
    this.setData({
      current: (Number(current) + 1)
    })
    this.getData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})